using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

public class UserModel
{
	public long id;
	public int serverId;
	public string account;										// 계정 
	public string name;											// 이름
	public short platformType;									// 플랫폼 타입
	public string paltformToken;								// 플랫폼 토큰
	public int state;											// 상태
	public int sectorUnion; 									// 소속연합ID
	public int level;											// 레벨 
	public int exp;												// 경험치
	public int cash;											// 캐쉬
	public int kina;											// 화폐단위(골드)
	public short orderPoint;									// 명령 횟수 (스테미나)
    public short maxOrderPoint;
    public short winPoint;
    public short abyssPoint;
	public Regions regions;
	public short friendCount;									// 친구 수
	public List<Item> inventory = new List<Item>();

    private List<MemberModel> m_Members = new List<MemberModel>();
    public List<MemberModel> members
    {
        get { return m_Members; }
        set { m_Members = value; GetMembersNoList(); }
    }

    [NonSerialized]         // 구 members (Dictionary -> List 로 변경후 이전 코드 사용 위해 wrapper 구현)
    public Dictionary<string, MemberModel> members_dic = new Dictionary<string, MemberModel>();    
 

    private List<int> m_choiceMembers = new List<int>();
    public List<int> choiceMembers
    {
        get { return m_choiceMembers; }
        set { m_choiceMembers = value; SetChoiceMembersList(); }
    }

    [NonSerialized]
    public int m_FormationPage = 0; 

    private List<int> m_MemberBattleArray = null;  // 21개 진영 3페이지 저장값
    public List<int> memberBattleArray
    {
        get {
            if (m_MemberBattleArray == null || m_MemberBattleArray.Count != Constants.FormationNum * Constants.FormationKind)
            {
                m_MemberBattleArray = new List<int>();
                for(int i = 0 ; i < Constants.FormationNum * Constants.FormationKind ; i++)
                {
                    m_MemberBattleArray.Add(0);
                }
            }
            return m_MemberBattleArray;
        }
        set { m_MemberBattleArray = value; }
    }

    public Dictionary<string, MemberModel> memStation = new Dictionary<string, MemberModel>();
	public DispatchResultModel dispatchResult;
	public QuickPvpInfo quickPvpInfo;
    public QuestResponseModel questInfo;
	public long newAlarmCount;
	public long lastAccessed;
	public long lastLogin;
	public long regDate;
    public int achiCompleteCount;
	public bool isPowerShards;
	public bool isProtected;

	public static UserModel Instance;

    [NonSerialized]
    public int SoulStone = 1000; 

    [NonSerialized]
    public List<MemberModel> m_ChoiceMemberList = new List<MemberModel>();

    [NonSerialized]
    public List<int> m_MemberNoList = new List<int>();


    [NonSerialized]
    public GameObject[] m_3DmodelWithFormation = new GameObject[Constants.FormationNum];    // 20인 포메이션에 설정된 케릭터 재활용
	public bool HasFormation()
	{
		for(int i=0; i<m_3DmodelWithFormation.Length; i++) {
			if(m_3DmodelWithFormation[i]!=null)
				return true;
		}
		return false;
	}

    [NonSerialized]
    public List<DPartySync> m_PartySincSuccessList = new List<DPartySync>();    //적용 되는 파티 싱크로 리스트
    void GetPartySyncSuccessList()
    {
        m_PartySincSuccessList.Clear();
        foreach (DPartySync tempPartySyncData in GameData.Inst.partySync)
        {          
            List<MemberModel> memList = new List<MemberModel>(m_ChoiceMemberList);
           
            if(SyncCheck(memList, tempPartySyncData.traits))
                m_PartySincSuccessList.Add(tempPartySyncData);
        }
    }

    
    bool SyncCheck(List<MemberModel> memModel, List<int> syncList)
    {       
        int havedId = -1;
        MemberModel tModel = memModel[0];

        bool isBreak = false;
        foreach(int tId in syncList)
        {
            isBreak = false;
            foreach(short tTrait in tModel.traits)
            {
                if(tId == (int)tTrait)
                {
                    havedId = tId;
                    isBreak = true;                    
                    break;
                }
            }
            if (isBreak)
                break;
        }


        if (havedId != -1)
        {
            memModel.Remove(tModel);
            syncList.Remove(havedId);

            if (syncList.Count == 0)
                return true;
            else
            {
                if (memModel.Count > 0)
                    return SyncCheck(memModel, syncList);
                else
                    return false;
            }
        }
        else
        {
            memModel.Remove(tModel);
            if (memModel.Count > 0)
                return SyncCheck(memModel, syncList);
            else
                return false;
        }
    }


    void GetMembersNoList()     //키값과 벨류의 no 값이 같음, 즉 키값 가져오는 함수
    {
        m_MemberNoList.Clear();
        members_dic.Clear();

        foreach (MemberModel temp in m_Members)
        {
            m_MemberNoList.Add(temp.no);
            members_dic.Add(temp.no.ToString(), temp);
        }        
    }

	public void SetChoiceMembersList()     // 전투 선택된 4 케릭터 리프레쉬
	{       
        m_ChoiceMemberList.Clear();

        foreach (KeyValuePair<string, MemberModel> temp in members_dic)
        {
            temp.Value.IsSelected = false;
        }

		foreach(int memNo in choiceMembers)
        {
			string strKey = memNo.ToString();
           
            if (members_dic.ContainsKey(strKey))
            {
                m_ChoiceMemberList.Add(members_dic[strKey]);
                members_dic[strKey].IsSelected = true;
            }
		}

        GetPartySyncSuccessList();
	}
}